package com.zt.ftphelper.view;

import com.zt.ftphelper.Config;
import com.zt.ftphelper.Main;
import com.zt.ftphelper.model.ServerItem;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;

import static com.zt.ftphelper.Config.DB_CLICK;

/**
 * @author zt
 * @Description:
 * @date 23/08/2019 17:47
 */
public class SettingsController {

    private Main main;
    private Stage dialogStage;
    private ObservableList<ServerItem> serverData = FXCollections.observableArrayList();


    @FXML
    private TextField basePathField;

    @FXML
    private TextField flashFxpLocationField;

    @FXML
    private TableView<ServerItem> serverTable;

    @FXML
    private TableColumn<ServerItem, String> ipColumn;

    @FXML
    private TableColumn<ServerItem, Integer> portColumn;

    @FXML
    private TableColumn<ServerItem, String> userNameColumn;

    @FXML
    private TableColumn<ServerItem, String> passwordColumn;

    @FXML
    private TableColumn<ServerItem, String> deployPathColumn;


    @FXML
    private void initialize(){
       basePathField.setText(Config.BASE_PATH);
       flashFxpLocationField.setText(Config.FLASHFXP_LOCATION);
       serverData =Config.getInstance().getServerData();
       buildTable();
    }

    private void buildTable() {
        serverTable.setItems(serverData);
        ipColumn.setCellValueFactory(new PropertyValueFactory<>("ip"));
        portColumn.setCellValueFactory(new PropertyValueFactory<>("port"));
        userNameColumn.setCellValueFactory(new PropertyValueFactory<>("userName"));
        passwordColumn.setCellValueFactory(new PropertyValueFactory<>("password"));
        deployPathColumn.setCellValueFactory(new PropertyValueFactory<>("deployPath"));
        serverTable.setRowFactory(tv -> {
            TableRow<ServerItem> row = new TableRow<>();
            row.setOnMouseClicked(event -> {
                ServerItem serverItem = row.getItem();
                if (event.getClickCount() == DB_CLICK) {
                    showServerEditDialog(serverItem);
                }
            });
            return row;
        });
    }

    @FXML
    private void selectBasePath(){
        DirectoryChooser directoryChooser=new DirectoryChooser();
        File file = directoryChooser.showDialog(dialogStage);
        directoryChooser.setTitle("选择起始路径");
        if(file!=null&&file.exists()&&!file.isHidden()){
            basePathField.setText( file.getPath());
        }
    }

    @FXML
    private void selectFlashFxpLocation(){
        FileChooser fileChooser=new FileChooser();
        FileChooser.ExtensionFilter extFilter =
                new FileChooser.ExtensionFilter("exe files (*.exe)", "*.exe");
        fileChooser.getExtensionFilters().add(extFilter);
        File file = fileChooser.showOpenDialog(dialogStage);
        fileChooser.setTitle("选择FlashFxp的路径");
        if(file!=null&&file.exists()&&file.canExecute()){
            flashFxpLocationField.setText( file.getPath());
        }

    }

    public boolean showServerEditDialog(ServerItem serverItem){
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(SettingsController.class.getResource("../view/ServerEditDialog.fxml"));
            AnchorPane page = loader.load();

            Stage stage = new Stage();
            if(serverItem!=null){
                stage.setTitle("编辑服务器");
            }else {
                stage.setTitle("添加服务器");

            }

            stage.initModality(Modality.WINDOW_MODAL);
            stage.setResizable(false);
            stage.initOwner(dialogStage);
            Scene scene = new Scene(page);
            stage.setScene(scene);
            ServerEditDialogController controller = loader.getController();
            controller.setDialogStage(stage);
            controller.setServerItem(serverItem);
            controller.setSettingsController(this);
            stage.showAndWait();
            return controller.isOkClicked();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


    @FXML
    private void handleOk(){

        Config.BASE_PATH =basePathField.getText();
        System.out.println("Set basePath to:"+Config.BASE_PATH);
        Config.FLASHFXP_LOCATION = flashFxpLocationField.getText();
        Config.getInstance().addString("defaultBasePath",basePathField.getText());
        if(flashFxpLocationField.getText()!=null){
            Config.getInstance().addString("flashFxpLocation",flashFxpLocationField.getText());
        }
        Config.getInstance().setServerData(serverData);

        main.refreshTree();
        Config.store();
        dialogStage.close();
    }

    @FXML
    private void handleCancel(){
        dialogStage.close();
    }

    public void setDialogStage(Stage dialogStage) {
        this.dialogStage = dialogStage;
    }

    public ObservableList<ServerItem> getServerData() {
        return serverData;
    }

    public void setMain(Main main) {
        this.main = main;
    }
}
